package com.sskj.gcc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.SellRecords;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.lib.base.BaseActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/07/30 16:06:36
 */
public class SellRecordsActivity extends BaseActivity<SellRecordsPresenter> {


    @BindView(R.id.toolBar)
    ToolBarLayout toolBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    BaseAdapter<SellRecords> recordsBaseAdapter;

    private Map<Integer, String> statusMap = new HashMap<>();

    int page = 1;

    @Override
    public int getLayoutResId() {
        return R.layout.app_activity_sell_records;
    }

    @Override
    public SellRecordsPresenter getPresenter() {
        return new SellRecordsPresenter();
    }

    @Override
    public void initView() {
        statusMap.put(1, App.INSTANCE.getString(R.string.app_sellRecordsActivity1));
        statusMap.put(2, App.INSTANCE.getString(R.string.app_sellRecordsActivity2));
        statusMap.put(3, App.INSTANCE.getString(R.string.app_sellRecordsActivity3));

        toolBar.setLeftButtonOnClickLinster(v -> finish());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recordsBaseAdapter = new BaseAdapter<SellRecords>(R.layout.app_item_sell_records, null, recyclerView) {
            @Override
            public void bind(ViewHolder holder, SellRecords item) {
                holder.setText(R.id.tv_time, TimeFormatUtil.SF_FORMAT_E.format(item.getAddtime()*1000))
                        .setText(R.id.tv_name, item.getUsername())
                        .setText(R.id.tv_status, statusMap.get(item.getState()))
                        .setText(R.id.tv_bank_num, item.getBank_num())
                        .setText(R.id.tv_bank, item.getBank_name() + "  " + item.getBank_open())
                        .setText(R.id.tv_sell_count, item.getNum() + "  USDT");
                if (item.getChecktime()==0){
                    holder.setText(R.id.tv_check_time,"--");
                }else {
                    holder .setText(R.id.tv_check_time, TimeFormatUtil.SF_FORMAT_E.format(item.getChecktime()*1000));
                }
                if (item.getReason() == null || item.getReason().isEmpty()) {
                    holder.getView(R.id.err_layout).setVisibility(View.GONE);
                } else {
                    holder.getView(R.id.err_layout).setVisibility(View.VISIBLE);
                    holder.setText(R.id.err_reason, item.getReason());
                }
            }
        };
    }

    @Override
    public void initData() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnLoadMoreListener(refreshLayout1 -> {
            mPresenter.getSellRecords(++page);
        });
        refreshLayout.setOnRefreshListener(refreshLayout1 -> {
            page = 1;
            mPresenter.getSellRecords(page);
        });

        mPresenter.getSellRecords(page);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SellRecordsActivity.class);
        context.startActivity(intent);
    }


    public void stopRefresh() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();

    }

    public void setData(List<SellRecords> data) {
        if (page == 1) {
            recordsBaseAdapter.setNewData(data);
        } else {
            recordsBaseAdapter.addData(data);
        }
    }
}
